package com.liu.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class ES索引操作 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient rest = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

//        DeleteIndexRequest requst = new DeleteIndexRequest("user");
//        CreateIndexRequest requst = new CreateIndexRequest("user");
        GetIndexRequest request = new GetIndexRequest("user");
        //删除
//        AcknowledgedResponse res = rest.indices().delete(requst, RequestOptions.DEFAULT);

        //创建
//        CreateIndexResponse res = rest.indices().create(requst, RequestOptions.DEFAULT);

        //查看
        GetIndexResponse res = rest.indices().get(request, RequestOptions.DEFAULT);

        System.out.println(res.getSettings());

        rest.close();
    }
    public void opera_Index() throws IOException {
        RestHighLevelClient test = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        //创建索引
        CreateIndexRequest create = new CreateIndexRequest("user");

        //查看索引
        GetIndexRequest get = new GetIndexRequest("user");

        //删除索引
        DeleteIndexRequest delete = new DeleteIndexRequest("user");

        //发送创建索引请求
        CreateIndexResponse createIndex = test.indices().create(create, RequestOptions.DEFAULT);

        //发送获取索引请求
        GetIndexResponse getIndex = test.indices().get(get, RequestOptions.DEFAULT);

        //发送删除索引的请求
        AcknowledgedResponse deleteIndex = test.indices().delete(delete, RequestOptions.DEFAULT);


        test.close();
    }
}
